<?php

namespace App\Admin\Controllers;

use \App\Models\GoodsType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Dropdown;

class GoodsTypeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GoodsType(), function (Grid $grid) {

            $grid->column('id')->sortable();
            $grid->column('type_name');
            $grid->column('p_id');
//            $grid->column('status', '状态')->display(function ($e) {
//                return $e == 1 ? '启用' : '未启用';
//            });
            $grid->status
                ->using([1 => '启用', 0 => '未启用'])
                ->dot(
                    [
                        1 => 'success',
                        0 => 'danger',
                    ],
                    'primary' // 默认颜色
                );
            $grid->column('icon','图片')->image('',100,100);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['type_name','status']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GoodsType(), function (Show $show) {
            $show->field('id');
            $show->field('type_name');
            $show->field('p_id','父级');
            $show->field('icon')->image('',100,100);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GoodsType(), function (Form $form) {
            $form->display('id');
            $form->text('type_name');
            $form->select('p_id')->options('type');
            $form->image('icon','图片')->disk('qiniu');
            $form->radio('status', '状态')->options(['1' => '启用', '0' => '未启用']);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function type()
    {
        $data = \App\Models\GoodsType::select('id','type_name')->get();
        foreach ($data as $k=>$v){
            $province[$k]['id'] = $v['id'];
            $province[$k]['text'] = $v['type_name'];
        }

        return $province;
    }
}
